import jwt from 'jsonwebtoken'

// 开始连接
export default async (ctx, next) => {
  const { userName, pwd } = ctx.request.body
  let res = await ctx.db.querys(`select * from user where userName='${userName}';`);
  if (!res.length) {
    ctx.body = { code: 0, msg: '未找到该账号', data: [] }
    return
  }
  if (res[0].pwd !== pwd) {
    ctx.body = { code: 0, msg: '密码错误', data: [] }
    return
  }
  let token = jwt.sign({ ...res[0], timer: new Date().getTime() }, 'jwup')



  await ctx.db.querys(`update user set loginTime='${new Date().getTime()}' where userName='${userName}'`)
  ctx.cookies.set('token', token, { httpOnly: false })
  ctx.body = { code: 1, msg: '登陆成功', data: res }
}